package cn.sccl.dirms.dev.service.impl;

import cn.sccl.common.database.DataSet;
import cn.sccl.common.service.impl.BaseManagerImpl;
import cn.sccl.dirms.dev.model.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sccl.common.dao.BaseDAO;
import cn.sccl.dirms.dev.service.MurriManager;
import cn.sccl.dirms.dev.dao.MurriDAO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 的Manager实现类
 * Created by liupan on 2017-06-25.
 */
@Service
public class MurriManagerImpl extends BaseManagerImpl<Murri> implements MurriManager {

    @Autowired
    private MurriDAO murriDAO;

    public MurriDAO getMurriDAO() {
        return murriDAO;
    }

    @Override
    public BaseDAO<Murri> getBaseDAO() {
        return getMurriDAO();
    }


    @Override
    public List<Graphs> listGraph(String grsNo) {
        return null;
    }

    @Override
    public List<Chart> listChart(String grsId) {
        return null;
    }

    @Override
    public List<ChartData> getSerData(String serId) {
        return null;
    }

    @Override
    public List<ChartSer> listChartSer(String chartId) {
        return null;
    }

    /**
     * 获取图形集合
     *
     * @param graphNo
     * @return
     */
    @Override
    public List<Murri> getGraph(String graphNo) {
        Map findm = new HashMap();
        findm.put("graphNo", graphNo);
        List<Murri> result = this.findBy(findm);
        return result;
    }

    /**
     * 查询详细数据 --动态执行sql
     *
     * @param id
     * @param query
     * @return
     * @throws Exception
     */
    @Override
    public ChartData viewData(String id,ChartQuery query) throws Exception {
        Murri murri = this.get(id);
        if(query==null){
            query = new ChartQuery();
        }
        return null;
    }

    /**
     * 获取rest数据
     * @param murri
     * @param query
     * @return
     */
    public ChartData getRestData(Murri murri,ChartQuery query){
        return null;
    }

    /**
     * 获取数据库数据
     *
     * @param murri
     * @param query
     * @return
     */
    public ChartData getDataBaseData(Murri murri,ChartQuery query){

        DataSet ds = getDataBase().query(murri.getGsql(),query.toArray());
        ChartData result = new ChartData();
        Map<String, Integer> filedMap;
        Series series = new Series();
        List<Object> x = new ArrayList<Object>();
        List<Object> y = new ArrayList<Object>();
        for (int i = 0; i < ds.RowCount; i++) {
            filedMap = ds.FieldsIndex;
            for (Map.Entry entry : filedMap.entrySet()) {
                //组装x轴值:两种情况，固定值/动态值,动态值从sql中取字段归类得出，固定值从配置字段中获取
                if(murri.getXcol().equalsIgnoreCase(entry.getKey().toString())){
                    x.add(ds.getRow(i).getObject((Integer)entry.getValue()));
                }
                //组装y轴值:两种情况，固定值/动态值,动态值从sql中取字段归类得出，固定值从配置字段中获取
                if(murri.getYcol().equalsIgnoreCase(entry.getKey().toString())){
                    y.add(ds.getRow(i).getObject((Integer) entry.getValue()));
                }
            }
        }
        series.setType("");
        series.setName("");
        series.setData(y);
        result.setXdata(x);
        result.setYdata(y);
        result.setSeries(series);
        result.setType(murri.getGraphType());
        result.setTitle(murri.getGraphTitle());
        result.setAutoRef(murri.getAutoRef());
        result.setRefMs(murri.getRefMs());
        return result;


    }
}